﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Data;
using System.Xml.Xsl;

class XL_XML
{
    private static string chuoiXML = "";
    private static XmlDocument taiLieu = null;

    public static string ChuoiXML
    {
        get { return XL_XML.chuoiXML; }
        set { XL_XML.chuoiXML = value; }
    }
    public static XmlDocument TaiLieu
    {
        get { return XL_XML.taiLieu; }
    }

    #region Nhom ham doc
    public static bool DocTaiLieu()
    {
        bool kq = true;
        taiLieu = new XmlDocument();

        try
        {
            taiLieu.LoadXml(chuoiXML);
        }
        catch 
        {
            kq = false;
            taiLieu = null;
        }
        
        return kq;
    }

    public static XmlNodeList DocDanhSach(string xPath)
    {
        if (taiLieu == null || xPath == null)
            return null;

        XmlNodeList danhSach;

        try
        {
            danhSach = taiLieu.SelectNodes(xPath);
        }
        catch (Exception ex)
        {
            danhSach = null;
        }

        return danhSach;
    }

    public static XmlElement DocNut(string xPath)
    {
        if (taiLieu == null || xPath == null)
            return null;

        XmlElement nut;

        try
        {
            nut = (XmlElement) taiLieu.SelectSingleNode(xPath);
        }
        catch (Exception ex)
        {
            nut = null;
        }

        return nut;
    }
    #endregion

    #region Nhom ham ghi
    public static string GhiTaiLieu()
    {
        if (taiLieu == null)
            return null;

        return taiLieu.OuterXml;
    }

    public static bool GhiDanhSach(string xPath, XmlNodeList danhSach)
    {
        if (taiLieu == null || xPath == null || danhSach == null)
            return false;

        bool kq = true;
        try
        {
            XmlNode nutCha = taiLieu.SelectSingleNode(xPath);
            for (int i = 0; i < danhSach.Count; i++)
                nutCha.AppendChild(danhSach[i]);
        }
        catch (Exception ex)
        {
            kq = false;
        }

        return kq;
    }

    public static bool GhiDanhSach(XmlNode nutCha, XmlNodeList danhSach)
    {
        if (taiLieu == null || nutCha == null || danhSach == null)
            return false;

        bool kq = true;
        try
        {
            for (int i = 0; i < danhSach.Count; i++)
                nutCha.AppendChild(danhSach[i]);
        }
        catch (Exception ex)
        {
            kq = false;
        }

        return kq;
    }

    public static bool GhiNut(string xPath, XmlNode nut)
    {
        if (taiLieu == null || xPath == null || nut == null)
            return false;

        bool kq = true;
        try
        {
            XmlNode nutCha = taiLieu.SelectSingleNode(xPath);
            nutCha.AppendChild(nut);
        }
        catch (Exception ex)
        {
            kq = false;
        }

        return kq;
    }

    public static bool GhiNut(XmlNode nutCha, XmlNode nut)
    {
        if (taiLieu == null || nutCha == null || nut == null)
            return false;

        bool kq = true;
        try
        {
            nutCha.AppendChild(nut);
        }
        catch (Exception ex)
        {
            kq = false;
        }

        return kq;
    }

    public static bool XoaNut(XmlNode nut)
    {
        if (taiLieu == null || nut == null)
            return false;

        bool kq = true;
        try
        {
            XmlNode nutCha = nut.ParentNode;
            nutCha.RemoveChild(nut);
        }
        catch (Exception ex)
        {
            kq = false;
        }
        return kq;
    }
    #endregion

    #region Nhom ham xuat
    public static void XuatRaCay(string thuocTinh, TreeView cay)
    {
        if (taiLieu == null || cay == null)
            return;

        cay.Nodes.Clear();
        try
        {
            XmlElement nutGoc = taiLieu.DocumentElement;
            XmlNodeList danhSachNut = nutGoc.ChildNodes;
            for (int i = 0; i < danhSachNut.Count; i++)
            {
                XmlElement nut = (XmlElement)danhSachNut[i];
                TreeNode nutCay = new TreeNode(nut.Attributes[thuocTinh].Value.ToString());
                nutCay.Tag = nut;
                cay.Nodes.Add(nutCay);
                DFS(nut, thuocTinh, nutCay);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
    }

    private static void DFS(XmlElement nut, string thuocTinh, TreeNode nutCay)
    {
        XmlNodeList danhSachNut = nut.ChildNodes;
        for (int i = 0; i < danhSachNut.Count; i++)
        {
            XmlElement nutCon = (XmlElement)danhSachNut[i];
            TreeNode nutCayCon = new TreeNode(nutCon.Attributes[thuocTinh].Value.ToString());
            nutCayCon.Tag = nutCon;
            nutCay.Nodes.Add(nutCayCon);
            DFS(nutCon, thuocTinh, nutCayCon);
        }
    }

    public static void XuatRaDanhSachChon(string xPath, string thuocTinh, ComboBox danhSachChon)
    {
        if (taiLieu == null || xPath == null || thuocTinh == null || danhSachChon == null)
            return;

        try
        {
            XmlNodeList danhSachNut = taiLieu.SelectNodes(xPath);
            for (int i = 0; i < danhSachNut.Count; i++)
            {
                danhSachChon.Items.Add(danhSachNut[i]);
            }
            danhSachChon.DisplayMember = thuocTinh;
            danhSachChon.ValueMember = thuocTinh;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
    }

    public static void XuatRaLuoi(string xPath, DataGridView luoi)
    {
        if (taiLieu == null || xPath == null || luoi == null)
            return;

        luoi.Rows.Clear();
        try
        {
            XmlNodeList danhSachNut = taiLieu.SelectNodes(xPath);
            int soThuocTinh = danhSachNut[0].Attributes.Count;
            if (soThuocTinh != luoi.ColumnCount)
                return;
            for (int i = 0; i < danhSachNut.Count; i++)
            {
                DataGridViewRow dongDuLieu = new DataGridViewRow();
                dongDuLieu.CreateCells(luoi);
                for (int j = 0; j < soThuocTinh; j++)
                {
                    dongDuLieu.Cells[j].Value = danhSachNut[i].Attributes[j].Value;
                }
                luoi.Rows.Add(dongDuLieu);
                luoi.Rows[luoi.RowCount - 1].Tag = (XmlElement)danhSachNut[i];
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
    }
    #endregion

    #region Nhom ham nhap
    public static void NhapTuLuoi(DataGridView luoi)
    {
        if (luoi == null || taiLieu == null || luoi.RowCount == 0 || luoi.ColumnCount == 0)
            return;
        try
        {
            int soThuocTinh = ((XmlElement)luoi.Rows[0].Tag).Attributes.Count;
            XmlNode nutCha = ((XmlElement)luoi.Rows[0].Tag).ParentNode;
            for (int i = 0; i < luoi.Rows.Count; i++)
            {
                XmlElement nut = (XmlElement)luoi.Rows[i].Tag;
                for (int j = 0; j < soThuocTinh; j++)
                {
                    nut.Attributes[j].Value = luoi[j, i].Value.ToString();
                }
                nutCha.AppendChild(nut);
            }
            taiLieu.AppendChild(nutCha);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
    }
    #endregion

    #region XSLT
    public static void ChuyenDoi(string tapTinNguon, string tapTinXSLT, string tapTinDich)
    {
        XmlDocument taiLieuNguon = new XmlDocument();
        taiLieuNguon.Load(tapTinNguon);

        XslCompiledTransform xsl = new XslCompiledTransform();
        XmlTextWriter wt = new XmlTextWriter(tapTinDich, System.Text.UTF8Encoding.UTF8);

        try
        {
            xsl.Load(tapTinXSLT);
            xsl.Transform(taiLieuNguon, null, wt);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            wt.Close();
        }
    }
    #endregion
}

